import React,{useEffect,useState} from 'react'
import '../../assets/homeDetail/index.less'
import Icon from '@/components/BaseComponent/Icon'
import {GetGoodsInfo} from '../../api/index'
import {PostAddCartList} from '../../api/cart'
import { history } from 'umi';
import { Toast } from 'antd-mobile'
// import {PostcreateOrder} from '../../api/createOrder'

function ProductDetail(props:any) {
  useEffect(() => {
    let axiosId = props.history.location.state
    GetGoodsInfo({goods_id:axiosId}).then(res=>{//请求详情接口
      setdetailObj(detailObj=res)
      // console.log(detailObj)
    })
  },[])
  let [detailObj,setdetailObj] = useState<any>({})
  let axiosId = props.history.location.state
  let addCartAxios = (num:any,axiosId:any) =>{
    // console.log(axiosId,num)
    PostAddCartList({//请求加入购物车接口
      goods_id:axiosId,
      goods_num:num
    }).then(res=>{
      if ((res as any).code === 1) {
        Toast.success((res as any).msg, 1)
      } else {
        Toast.success((res as any).msg, 1)
      }
    })
  }
  let num = 0
  return (
    <div className='home_detail'>
        <div className='home_detail_img'>
            <img src={detailObj.goodsimage_text} alt=""/>
        </div>
        <div className='home_detail_price'>
          <span>价格：</span><a>¥{detailObj.price}</a>
        </div>
        <div className='home_detail_font'>
          {detailObj.status_text}
        </div>
        <div  className='home_detail_foot'>
            <div  className='home_detail_foot_home_font'>
              <Icon className='shouye'/>
              <span onClick={() => {
               history.push('/cart') 
              }}>购物车</span>
            </div>
            <div className='home_detail_foot_home_btn'>
              <button className='btn_one' onClick={()=>{
                // props.history.push('/orderConfirm')
                // console.log()
                // PostcreateOrder({
                //   car_id:[8,9],
                //   price:1.00
                // }).then(res=>{
                //   console.log(res)
                // })
              }}>立即购买</button>
              <button className='btn_two' onClick={()=>{
                num++
                // console.log(num,axiosId)
                addCartAxios(num,axiosId)
              }}>加入购物车</button>
            </div>
        </div>
    </div>
  )
}

export default ProductDetail
